package com.handpay.zztong.hp.swiper.activity;

import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.swiper.ApiSwiperFactory;
import com.handpay.framework.swiper.ISwiperListener;
import com.handpay.framework.swiper.SwiperModel;
import com.handpay.framework.swiper.landi.AipSharedPreferences;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.ZZTApplication;
import com.handpay.zztong.hp.base.activity.BaseActivity;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.config.ZZTConstant;
import com.handpay.zztong.hp.log.HPLog;
import com.handpay.zztong.hp.permission.PermissionRequestCode;
import com.handpay.zztong.hp.swiper.SwiperUtils;
import com.handpay.zztong.hp.swiper.model.SwiperBluetoothDeviceSelectInfo;
import com.handpay.zztong.hp.swiper.model.SwiperDeviceSelectInfo;
import com.handpay.zztong.hp.swiper.widget.BindDeviceVoiceLayoutView;
import com.handpay.zztong.hp.swiper.widget.SwiperBluetoothSelectView;
import com.handpay.zztong.hp.swiper.widget.SwiperBluetoothSelectView.OnSwiperBluetoothSelectOnItemClickListenter;
import com.handpay.zztong.hp.ui.SwiperSelectView;
import com.handpay.zztong.hp.ui.SwiperSelectView.OnSwiperSelectOnClickListenter;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.lang.ref.WeakReference;
import java.util.List;

/***
 * 刷卡器连接插件、包含了刷卡器选择、蓝牙选择、音频和蓝牙连接动画界面
 *
 * @author dlfeng
 */
public class ConnectSwiperActivity extends ZZTongActivity {

    private static final String TAG = "ConnectSwiperActivity";

    public static final String INTENT_MSG = "isOnlySupportOneSwiper";
    // 默认选择刷卡器
    public static final int SWIPER_DEFUALT = 0x1 << 0;
    // 刷卡器选择
    public static final int SWIPER_SELECT = 0x1 << 1;
    // 蓝牙选择设备
    public static final int SWIPER_BLUETOOTH_SELECT = 0x1 << 2;
    // 音频连接动画界面
    public static final int SWIPER_VOICE_ANIMATION = 0x1 << 3;
    // 蓝牙连接动画界面
    public static final int SWIPER_BLUETOOTH_ANIMATION = 0x1 << 4;
    // 自动连接上一次连接过的刷卡器
    public static final int SWIPER_SELECT_AUTO_CONNECT = 0x1 << 5;

    private RelativeLayout swiperContent = null;

    private String DName = "";

    private SwiperModel swiperModel;
    private static int selectIndex = 2;
    private BroadcastReceiver mBroadcastReceiver;

    @Override
    public ActionBar getActivityActionBar() {
        // leftBar goBack();
        return new ActionBar(this.getString(R.string.swipe_select_title), true, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                returnToBeforeView();
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_connect_swiper);
        super.onCreate(savedInstanceState);
        swiperContent = (RelativeLayout) this.findViewById(R.id.swiperContent);
        /**
         * if(intent != null){ isOnlySwiper = intent.getBooleanExtra(INTENT_MSG,
         * false); if(isOnlySwiper){ int[] models =
         * getResources().getIntArray(R.array.swipe_model); swiperModel =
         * SwiperModel.values()[models[0]]; DName =
         * getResources().getStringArray(R.array.swipe_types)[0];
         * onSelectSwiper(); return; } }
         **/
        if ("HXZZT".equalsIgnoreCase(ClientEngine.CHANNEL)) {
            selectIndex = 1;
        }
        if (this.getIntent().getIntExtra("SWIPER_STATE", -1) == SWIPER_SELECT_AUTO_CONNECT) {
            refreshStatus(SWIPER_SELECT_AUTO_CONNECT);
        } else {
            refreshStatus(SWIPER_SELECT);
        }

        // yanglun 2.1.6
        // DeviceInfo deviceInfo =
        // AipSharedPreferences.getInstance(this).getDeviceInfo();
        // if (deviceInfo != null ){
        // refreshStatus(SWIPER_SELECT_AUTO_CONNECT);
        // }else{

        // }
    }


    @Override
    public void onSucceed(int requestCode, List<String> grantPermissions) {
        if (requestCode == PermissionRequestCode.REQUEST_CODE_AUDIO_BLUETOOTH) {
            Toast.makeText(ConnectSwiperActivity.this, R.string.message_audio_blueth_succesed, Toast.LENGTH_SHORT).show();
        }else if (requestCode==PermissionRequestCode.REQUEST_CODE_PERMISSION_LOCATION){
            Toast.makeText(ConnectSwiperActivity.this, R.string.message_location_succeed, Toast.LENGTH_SHORT).show();
            //由于首先进行的是定位权限检查，因此开启后继续检查蓝牙权限
            checkAudioBluetoothPermission();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        HPLog.d(HPLog.SSX_TAG, "onActivityResult:" + requestCode);
        if (requestCode == PermissionRequestCode.REQUEST_CODE_AUDIO_BLUETOOTH) {
            Toast.makeText(ConnectSwiperActivity.this, R.string.message_audio_blueth_failed, Toast.LENGTH_SHORT).show();
            checkAudioBluetoothPermission();
            /*if (!AndPermission.hasPermission(this, Manifest.permission.RECORD_AUDIO, Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN)) {
                AndPermission.with(this).requestCode(PermissionRequestCode.REQUEST_CODE_AUDIO_BLUETOOTH).permission(Manifest.permission.RECORD_AUDIO, Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN).rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        AndPermission.rationaleDialog(ConnectSwiperActivity.this, rationale).show();
                    }
                }).send();
            }*/
        }else if(requestCode==PermissionRequestCode.REQUEST_CODE_PERMISSION_LOCATION){
            Toast.makeText(ConnectSwiperActivity.this, R.string.message_location_failed, Toast.LENGTH_SHORT).show();
            checkLocationPermission();
        }
    }

    protected void refreshStatus(int status) {
        Bundle data = new Bundle();
        data.putInt("status", status);
        Message msg = new Message();
        msg.setData(data);
        hRefreshStatus.sendMessage(msg);
    }

    /**
     * 刷新状态
     */
    private Handler hRefreshStatus = new CSAHandler(this);

    private boolean isOnlySwiper = false;

    /**
     * 只负责显示界面。不做刷卡器操作
     */
    class CSAHandler extends Handler {
        private final WeakReference<ConnectSwiperActivity> mBaseActivity;

        public CSAHandler(ConnectSwiperActivity activity) {
            mBaseActivity = new WeakReference<ConnectSwiperActivity>(activity);
        }

        public BaseActivity getActivity() {
            return mBaseActivity.get();
        }

        @Override
        public void handleMessage(Message msg) {
            final ConnectSwiperActivity activity = (ConnectSwiperActivity) getActivity();

            if (null == activity) {
                return;
            }
            int status = msg.getData().getInt("status");
                switch (status) {
                case SWIPER_SELECT:// 选择刷卡器
                    HPLog.i(TAG, "选择刷卡器-------------");
                    SwiperSelectView ssv = new SwiperSelectView(activity, selectIndex);
                    ssv.setOnSelectClick(new OnSwiperSelectOnClickListenter() {
                        @Override
                        public void onSecletClick(String mDeviceName, int[] models, SwiperModel swiperModel, int position) {
                            boolean flag_location=true;
                            //如果选择的刷卡器不是音频的就去检查定位
                            if(!activity.isVoice(swiperModel)){
                                flag_location=checkLocationPermission();
                            }
                            if(flag_location){
                                boolean flag_AudioBluetooth=checkAudioBluetoothPermission();
                                if(flag_AudioBluetooth){
                                    HPLog.i(TAG, "mDeviceName : " + mDeviceName + "models : " + models + "swiperModel : " + swiperModel + "position : " + position);
                                    activity.DName = mDeviceName;
                                    activity.swiperModel = swiperModel;
                                    activity.onSelectSwiper();
                                    SwiperDeviceSelectInfo lastDevice = new SwiperDeviceSelectInfo();
                                    lastDevice.selectType = activity.isVoice(swiperModel) ? SwiperDeviceSelectInfo.DEVICE_TYPE_VOICE : SwiperDeviceSelectInfo.DEVICE_TYPE_BLUETOOTH;
                                    lastDevice.selectIndex = position;
                                    AipSharedPreferences.getInstance(activity).setLastSelectDevice(lastDevice);
                                }
                            }
                            /*if (!AndPermission.hasPermission(ConnectSwiperActivity.this, Manifest.permission.RECORD_AUDIO,
                                    Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN,Manifest.permission.ACCESS_COARSE_LOCATION)) {
                                AndPermission.with(ConnectSwiperActivity.this)
                                        .requestCode(PermissionRequestCode.REQUEST_CODE_AUDIO_BLUETOOTH)
                                        .permission(Manifest.permission.RECORD_AUDIO, Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN,Manifest.permission.ACCESS_COARSE_LOCATION)
                                        .rationale(new RationaleListener() {
                                    @Override
                                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                                        AndPermission.rationaleDialog(ConnectSwiperActivity.this, rationale).show();
                                    }
                                }).send();
                            }else {
                                HPLog.i(TAG, "mDeviceName : " + mDeviceName + "models : " + models + "swiperModel : " + swiperModel + "position : " + position);
                                activity.DName = mDeviceName;
                                activity.swiperModel = swiperModel;
                                activity.onSelectSwiper();
                                SwiperDeviceSelectInfo lastDevice = new SwiperDeviceSelectInfo();
                                lastDevice.selectType = activity.isVoice(swiperModel) ? SwiperDeviceSelectInfo.DEVICE_TYPE_VOICE : SwiperDeviceSelectInfo.DEVICE_TYPE_BLUETOOTH;
                                lastDevice.selectIndex = position;
                                AipSharedPreferences.getInstance(activity).setLastSelectDevice(lastDevice);
                            }*/
                        }
                    });
                    ssv.setTag(SWIPER_SELECT);
                    activity.addView(ssv);
                    break;
                case SWIPER_BLUETOOTH_SELECT:
                    String filter = activity.getBluetoothFilter();
                    if (TextUtils.isEmpty(filter)) {
                        Toast.makeText(activity, R.string.not_util_buletooth_filter, Toast.LENGTH_SHORT).show();
                    }
                    final SwiperBluetoothSelectView sbsv = new SwiperBluetoothSelectView(activity, filter, activity.DName);
                    sbsv.setmItemClickListener(new OnSwiperBluetoothSelectOnItemClickListenter() {

                        @Override
                        public void onSelectClick(String devicename, String deviceid, String deviceInfo) {
                            boolean flag_AudioBluetooth=checkAudioBluetoothPermission();
                            if(flag_AudioBluetooth){
                                HPLog.i(TAG, "devicename : " + devicename + "deviceid : " + deviceid + "deviceInfo : " + deviceInfo);
                                activity.refreshStatus(SWIPER_BLUETOOTH_ANIMATION);
                                activity.startSwiperConnect(deviceInfo);
                            }
                            /*if (!AndPermission.hasPermission(ConnectSwiperActivity.this, Manifest.permission.RECORD_AUDIO, Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN)) {
                                AndPermission.with(ConnectSwiperActivity.this).requestCode(PermissionRequestCode.REQUEST_CODE_AUDIO_BLUETOOTH).permission(Manifest.permission.RECORD_AUDIO, Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN).rationale(new RationaleListener() {
                                    @Override
                                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                                        AndPermission.rationaleDialog(ConnectSwiperActivity.this, rationale).show();
                                    }
                                }).send();
                            }else {
                                HPLog.i(TAG, "devicename : " + devicename + "deviceid : " + deviceid + "deviceInfo : " + deviceInfo);
                                activity.refreshStatus(SWIPER_BLUETOOTH_ANIMATION);
                                activity.startSwiperConnect(deviceInfo);
//                            AipSharedPreferences.SwiperDeviceSelectInfo lastDevice = AipSharedPreferences.getInstance(activity).getLastSelectDevice();
//                            lastDevice.selectBlueDeviceName = devicename;
//                            AipSharedPreferences.getInstance(activity).setLastSelectDevice(lastDevice);
                            }*/
                        }

                        @Override
                        public void OnCancelClick() {
                            activity.returnToBeforeView();
                            // activity.refreshStatus(SWIPER_VOICE_ANIMATION);
                        }

                        @Override
                        public void OnDiscoverComplete() {
                            activity.returnToBeforeView();
                        }
                    });
                    sbsv.setTag(SWIPER_BLUETOOTH_SELECT);
                    activity.addView(sbsv);
                    break;
                case SWIPER_VOICE_ANIMATION:
                    activity.myActionBar.refreshActionBar(new ActionBar("刷卡器连接"));
                    BindDeviceVoiceLayoutView bdcl = new BindDeviceVoiceLayoutView(activity);
                    bdcl.setTag(SWIPER_VOICE_ANIMATION);
                    activity.addView(bdcl);
                    break;
                case SWIPER_BLUETOOTH_ANIMATION:
                    activity.myActionBar.refreshActionBar(new ActionBar("刷卡器连接"));
                    View vlv = LayoutInflater.from(activity).inflate(R.layout.layout_wating_for_bind_devic_bluetooth, null);
                    vlv.setTag(SWIPER_VOICE_ANIMATION);
                    activity.addView(vlv);
                    break;
                case SWIPER_SELECT_AUTO_CONNECT://自动连接刷卡器
                    SwiperDeviceSelectInfo lastDevice = AipSharedPreferences.getInstance(activity).getLastSelectDevice();
                    HPLog.i(HPLog.YL_TAG, "auto connect SWIPER_SELECT_AUTO_CONNECT:" + lastDevice.toString());
                    if (lastDevice.selectType == SwiperDeviceSelectInfo.DEVICE_TYPE_VOICE) {
                        if (!SwiperUtils.isVoiceIn()) {
                            activity.refreshStatus(SWIPER_SELECT);
                            return;
                        }
                        // 音频
                        activity.refreshStatus(SWIPER_VOICE_ANIMATION);
                        int[] voiceDeviceArray = activity.getSwiperModle(R.array.voice_swiper_model);
                        int listViewIndex = AipSharedPreferences.getInstance(activity).getLastSelectDevice().selectIndex;
                        int deviceIndex = voiceDeviceArray[listViewIndex];
                        activity.swiperModel = SwiperModel.values()[deviceIndex];
                        activity.startSwiperConnect(null);
                    } else {
                        // 蓝牙

                        activity.refreshStatus(SWIPER_BLUETOOTH_ANIMATION);
                        int[] bluetoothDeviceArray = activity.getSwiperModle(R.array.bluetooth_swiper_model);
                        int listViewIndex = lastDevice.selectIndex;
//                        HPLog.i(HPLog.YL_TAG, "auto connect last device name:" + lastDevice.selectBlueDeviceName);

                        int deviceIndex = bluetoothDeviceArray[listViewIndex];
                        activity.swiperModel = SwiperModel.values()[deviceIndex];
                        HPLog.i(HPLog.YL_TAG, "auto connect BluetoothFilter:" + activity.getBluetoothFilter());
                        IntentFilter mBroadcastReceiverFilter = new IntentFilter();
                        mBroadcastReceiverFilter.addAction(BluetoothDevice.ACTION_FOUND);
                        mBroadcastReceiverFilter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
                        final BluetoothAdapter mBtAdapter = BluetoothAdapter.getDefaultAdapter();
                        mBroadcastReceiver = new BroadcastReceiver() {
                            boolean bDeviceFound = false;

                            @Override
                            public void onReceive(Context context, Intent intent) {
                                HPLog.i(HPLog.YL_TAG, "auto connect intent.getAction():" + intent.getAction());
                                if (intent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
                                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                                    String deviceName = device.getName();
                                    HPLog.i(HPLog.YL_TAG, "auto connect deviceName:" + deviceName);
                                    if (!TextUtils.isEmpty(deviceName) && deviceName.contains(activity.getBluetoothFilter())) {
                                        ZZTApplication.getInstance().bdDevice = device;
//                                        if (deviceInfo == null) {
//                                            AipSharedPreferences.getInstance(activity).setLastSelectDevice(null);
//                                            Toast.makeText(activity, activity.getString(R.string.no_bluetooth_device), Toast.LENGTH_SHORT).show();
//                                            activity.goBack();
//                                            unregisterReceiver(this);
//                                            return;
//                                        } else {
                                        SwiperBluetoothDeviceSelectInfo deviceInfo = AipSharedPreferences.getInstance(activity).getSwiperBluetoothDeviceInfo();

                                        if (deviceName.equals(deviceInfo.getName())) {
                                            bDeviceFound = true;
                                            mBtAdapter.cancelDiscovery();
                                            activity.startSwiperConnect(deviceInfo.getIdentifier());
                                            unregisterReceiver(this);
                                        }
//                                        }
                                        return;
                                    }
                                } else {
                                    if (bDeviceFound == false) {
                                        AipSharedPreferences.getInstance(activity).setLastSelectDevice(null);
                                        AipSharedPreferences.getInstance(activity).setBluetoothDeviceInfo(null);
                                        Toast.makeText(activity, activity.getString(R.string.no_bluetooth_device), Toast.LENGTH_SHORT).show();
                                        activity.goBack();
                                        unregisterReceiver(this);
                                    }
                                }
                            }
                        };
                        activity.registerReceiver(mBroadcastReceiver, mBroadcastReceiverFilter);
                        if (mBtAdapter.isEnabled()) {
                            if (mBtAdapter.isDiscovering()) {
                                mBtAdapter.cancelDiscovery();
                            }
                            // 并重新开始
                            mBtAdapter.startDiscovery();

                        } else {
                            Toast.makeText(activity, activity.getString(R.string.please_open_bluetooth), Toast.LENGTH_SHORT).show();
                        }

                    }
                    // activity.swiperModel = swiperModel;
                    // activity.startSwiperConnect(deviceInfo.getIdentifier());
                    break;
            }
        }
    }

    //检查定位权限（android6.0以上，蓝牙搜索需要开启定位）
    private boolean checkLocationPermission(){
        //这里最好不要写引用  小于M的版本里面没有23的引用
        if(Build.VERSION.SDK_INT<23){
            return true;
        }
        if (!AndPermission.hasPermission(ConnectSwiperActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
            AndPermission.with(ConnectSwiperActivity.this)
                    .requestCode(PermissionRequestCode.REQUEST_CODE_PERMISSION_LOCATION)
                    .permission(Manifest.permission.ACCESS_COARSE_LOCATION)
                    .rationale(new RationaleListener() {
                        @Override
                        public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                            AndPermission.rationaleDialog(ConnectSwiperActivity.this, rationale).show();
                        }
                    }).send();
            return false;
        }
        return true;
    }
    //检查蓝牙音频权限
    private boolean checkAudioBluetoothPermission(){
        if (!AndPermission.hasPermission(ConnectSwiperActivity.this, Manifest.permission.RECORD_AUDIO,Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN)) {
            AndPermission.with(ConnectSwiperActivity.this)
                    .requestCode(PermissionRequestCode.REQUEST_CODE_AUDIO_BLUETOOTH)
                    .permission(Manifest.permission.RECORD_AUDIO, Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN)
                    .rationale(new RationaleListener() {
                        @Override
                        public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                            AndPermission.rationaleDialog(ConnectSwiperActivity.this, rationale).show();
                        }
                    }).send();
            return false;
        }
        return true;
    }
    @Override
    protected void onStop() {
        super.onStop();


    }

    private int[] getSwiperModle(int resid) {
        return getResources().getIntArray(resid);
    }

    private String[] getSwiperType(int resid) {
        return getResources().getStringArray(resid);
    }

    private int getIndexFromArray(String[] deviceArray, String deviceName) {
        for (int i = 0; i < deviceArray.length; i++) {
            if (deviceArray[i].equals(deviceName)) {
                return i;
            }
        }
        return -1;
    }

    private void returnToBeforeView() {
        if (0 != swiperContent.getChildCount()) {
            Object obj = swiperContent.getChildAt(0).getTag();
            if (obj instanceof Integer) {
                int type = (Integer) obj;
                switch (type) {
                    case SWIPER_SELECT:
                        finish();
                        break;
                    case SWIPER_BLUETOOTH_SELECT:
                        if (!isOnlySwiper) {
                            selectIndex = 2;
                            refreshStatus(SWIPER_SELECT);
                        } else {
                            finish();
                        }
                        break;
                    case SWIPER_VOICE_ANIMATION:

                        break;
                    case SWIPER_BLUETOOTH_ANIMATION:

                        break;

                    default:
                        break;
                }
                return;
            }
        }

        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            returnToBeforeView();
            // super.onKeyDown(keyCode, event);
        }
        return true;
    }

    private void addView(View view) {
        if (0 != swiperContent.getChildCount()) {
            swiperContent.removeAllViews();
        }
        swiperContent.addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    private void onSelectSwiper() {
        SwiperUtils.cleanSwiperData();
        if (isVoice(swiperModel)) {
            // 音频
            if (!SwiperUtils.isVoiceIn()) {
                Toast.makeText(ConnectSwiperActivity.this, R.string.device_unplugged, Toast.LENGTH_SHORT).show();
                return;
            }
            refreshStatus(SWIPER_VOICE_ANIMATION);
            startSwiperConnect(null);
        } else {
            // 蓝牙
            refreshStatus(SWIPER_BLUETOOTH_SELECT);
        }
    }

    public void startSwiperConnect(String addition) {

        boolean result = ApiSwiperFactory.init(this, swiperModel, addition);
        if (result) {
            SwiperUtils.saveConnectedBTMac(addition);
            onSwiperStatus(ISwiperListener.Status_Connect, ISwiperListener.Swiper_Plug_In, null, false);
        } else {
            ApiSwiperFactory.getInstance().clearSwiper(null);
            AipSharedPreferences.getInstance(this).setLastSelectDevice(null);
            Toast.makeText(this, R.string.error_connect_bluetooth, Toast.LENGTH_SHORT).show();
            goBack();
        }
    }

    @Override
    public void onConnectSuccess() {
        HPLog.i("result", "---onConnectSuccess--");
        //主要解决因线程锁死导致下面的Toast不消失
        int code = getIntent().getIntExtra("SWIPER_STATE", 0);
        if (code != SWIPER_SELECT_AUTO_CONNECT) {
            Toast.makeText(this, R.string.device_succesed, Toast.LENGTH_SHORT).show();
        }
        setResult(RESULT_OK);
        goBack();
    }

    private String getBluetoothFilter() {
        String filter = null;
        switch (swiperModel) {
            case KOAL_BLUETOOTH: {
                filter = ZZTConstant.ZZT_NAME_FILTER;
                break;
            }
            case NEWLAND_BLUETOOTH: {
                filter = ZZTConstant.NEWLAND_NAME_FILTER;
                break;
            }
            // 新大陆ME15蓝牙POS
            case ME15_BLUETOOTH:
                filter = ZZTConstant.ME15_NAME_FILTER;
                break;
            // 新大陆ME15蓝牙POS
            case M18_BLUETOOTH:
                filter = ZZTConstant.M18_NAME_FILTER;
                break;
            case M35_BLUETOOTH:
                filter = ZZTConstant.M35_NAME_FILTER;
                break;
            // 新增联迪M36:2015/8/12
            case M36_BLUETOOTH:
                filter = ZZTConstant.M36_NAME_FILTER;
                break;
            case ME31_BLUETOOTH:
                filter = ZZTConstant.ME31_NAME_FILTER;
                break;
            // 新增 ME368:2015/4/1
            case ME368_BLUETOOTH:
                filter = ZZTConstant.ME368_NAME_FILTER;
                break;
            // 新增BBPOS M188蓝牙刷卡器:2015/11/03
            case M188_BLUETOOTH:
                filter = ZZTConstant.M188_NAME_FILTER;
                break;
            case M1218_BLUETOOTH:
                filter = ZZTConstant.M1218_NAME_FILTER;
                break;
            case I21B_BLUETOOTH:
                filter = ZZTConstant.I21B_NAME_FILTER;
                break;
            case P27_BLUETOOTH:
                filter = ZZTConstant.P27_NAME_FILTER;
                break;
            case M198_BLUETOOTH:
                filter = ZZTConstant.M198_NAME_FILTER;
                break;
            case P84_BLUETOOTH:
                filter = ZZTConstant.P84_NAME_FILTER;
                break;
            default:
                break;
        }
        return filter;
    }

}
